package sk.stuba.fiit.clientApp.gui.handler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;



import sk.stuba.fiit.clientApp.cvo.ArchiveCVO;
import sk.stuba.fiit.clientApp.cvo.ArchiveResultCVO;
import sk.stuba.fiit.clientApp.cvo.UserDetailCVO;
import sk.stuba.fiit.clientApp.gui.panel.BasicPanel;
import sk.stuba.fiit.clientApp.singeltons.ControllerLocatorSingelton;
import sk.stuba.fiit.ltasys.ws.schema.beans.ArchiveRequest;
import sk.stuba.fiit.ltasys.ws.schema.beans.ArchiveResponse;

public class ArchiveExecuteActionListener implements ActionListener {
	private BasicPanel panel;
	 private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	 private Pattern pattern;
	  private Matcher matcher;

	 
	public ArchiveExecuteActionListener(BasicPanel basicPanel) {
		this.panel = basicPanel;
		 pattern = Pattern.compile(EMAIL_PATTERN);
	}

	public boolean validate(final String hex){ 
		  matcher = pattern.matcher(hex);
		  return matcher.matches();
	  }
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String value = (String) panel.getComboBoxArchive().getModel()
				.getSelectedItem();
		ControllerLocatorSingelton application = ControllerLocatorSingelton
				.getInstance();
		try {
			ArchiveCVO archiveCvo = new ArchiveCVO();
			archiveCvo.setEmail(panel.getTfEmail().getText());
			archiveCvo.setFile(panel.getTfPackage().getText());
			archiveCvo.setSendArchive(panel.getChckbxSend().isSelected());
			archiveCvo.setSendRenew(panel.getChckbxSendRenew().isSelected());
			archiveCvo.setToken(panel.getTfToken().getText());
			archiveCvo.setUserId(panel.getTfUserID().getText());
			archiveCvo.setUserName(panel.getTfUserName().getText());
			archiveCvo.setUserPassword(panel.getTfUserPassword().getText());
			
			ArchiveResultCVO result = new ArchiveResultCVO();
			switch (value.charAt(0)) {
			case '1':
					if(!validate(panel.getTfEmail().getText())){
						JOptionPane.showMessageDialog(null,"Invalid email address!", "Error",JOptionPane.ERROR_MESSAGE);
						return;
					}
					result = application.getSignatureContreoller().archivePackage(archiveCvo);
					panel.getConsole().append(new Date()
					+ "\t\t"+result.getStatusInfo() +" User token is: "+result.getUserToken() +"\n");
				break;
			case '2':
					if(panel.getTfToken().getText().equals("")){
						JOptionPane.showMessageDialog(null,"Token must be entered!", "Error",JOptionPane.ERROR_MESSAGE);
						return;
					}
					 result = application.getSignatureContreoller().deleteArchivePackage(archiveCvo);
					 panel.getConsole().append(new Date()
						+ "\t\t"+result.getStatusInfo() +" for user token: "+result.getUserToken() +"\n");
				break;
			case '3':
					if(panel.getTfToken().getText().equals("")){
						JOptionPane.showMessageDialog(null,"Token must be entered!", "Error",JOptionPane.ERROR_MESSAGE);
						return;
					}
					result = application.getSignatureContreoller().obtainArchivePackage(archiveCvo);
					panel.getConsole().append(new Date()
							+ "\t\t"+result.getStatusInfo()+ " for user token: "+result.getUserToken()+"\n");
				break;
			case 'X':
				UserDetailCVO userCVO = new UserDetailCVO();
				userCVO.setUserId(panel.getTfUserID().getText());
				userCVO.setUsername(panel.getTfUserName().getText());
				userCVO.setUserPassword(panel.getTfUserPassword().getText());

				userCVO = application.getSignatureContreoller().setUserAccount(
						userCVO);

				setUserPreferencies(userCVO);
				panel.getConsole()
						.append(new Date()
								+ "\t\tUser preferencies was set saccessfully\n");

				break;
			}
		} catch (Exception e1) {
			panel.getConsole()
			.append(new Date()
					+ "\t\tError ocured during operation\n");
			e1.printStackTrace();
		}
	}

	private void setUserPreferencies(UserDetailCVO detail) {
		panel.getTfUserID().setText(detail.getUserId());
		panel.getTfUserID().setEnabled(false);
		panel.getTfUserName().setText(detail.getUsername());
		panel.getTfUserName().setEnabled(false);
		panel.getTfUserPassword().setText(detail.getUserPassword());
		panel.getTfUserPassword().setEnabled(false);
	}
}
